stringify helpers before bundling

pull/525/head
Rich-Harris 7 years ago
parent dee8694e3b
commit e796fefcee

@ -17,10 +17,10 @@
"precodecov": "npm run coverage",
"lint": "eslint src test/*.js",
"build": "npm run build:main && npm run build:shared && npm run build:ssr",
"build:main": "rollup -c rollup/rollup.config.main.js",
"build:main": "node shared/_build.js && rollup -c rollup/rollup.config.main.js",
"build:shared": "rollup -c rollup/rollup.config.shared.js",
"build:ssr": "rollup -c rollup/rollup.config.ssr.js",
"dev": "rollup -c rollup/rollup.config.main.js -w",
"dev": "node shared/_build.js && rollup -c rollup/rollup.config.main.js -w",
"dev:shared": "rollup -c rollup/rollup.config.shared.js -w",
"pretest": "npm run build",
"prepublish": "npm run lint && npm run build"

@ -6,7 +6,7 @@ import { walk } from 'estree-walker';
import deindent from '../../utils/deindent.js';
import CodeBuilder from '../../utils/CodeBuilder.js';
import visit from './visit.js';
import { nameMap, sharedMap } from './sharedNames.js';
import shared from './shared.js';
import Generator from '../Generator.js';
import preprocess from './preprocess.js';
@ -25,7 +25,7 @@ class DomGenerator extends Generator {
}
helper ( name ) {
if ( this.options.dev && sharedMap.has( `${name}Dev` ) ) {
if ( this.options.dev && `${name}Dev` in shared ) {
name = `${name}Dev`;
}
@ -275,20 +275,20 @@ export default function dom ( parsed, source, options ) {
);
} else {
generator.uses.forEach( key => {
const str = sharedMap.get( key );
const str = shared[ key ];
const code = new MagicString( str );
const fn = parseExpressionAt( str, 0 );
const expression = parseExpressionAt( str, 0 );
let scope = annotateWithScopes( fn );
let scope = annotateWithScopes( expression );
walk( fn, {
walk( expression, {
enter ( node, parent ) {
if ( node._scope ) scope = node._scope;
if ( node.type === 'Identifier' && isReference( node, parent ) && !scope.has( node.name ) ) {
if ( nameMap.has( node.name ) ) {
if ( node.name in shared ) {
// this helper function depends on another one
const dependency = nameMap.get( node.name );
const dependency = node.name;
generator.uses.add( dependency );
const alias = generator.alias( dependency );
@ -309,8 +309,8 @@ export default function dom ( parsed, source, options ) {
`var ${generator.alias( 'transitionManager' )} = window.${global} || ( window.${global} = ${code});`
);
} else {
const alias = generator.alias( fn.id.name );
if ( alias !== fn.id.name ) code.overwrite( fn.id.start, fn.id.end, alias );
const alias = generator.alias( expression.id.name );
if ( alias !== expression.id.name ) code.overwrite( expression.id.start, expression.id.end, alias );
builders.main.addBlock( code.toString() );
}

@ -1,12 +0,0 @@
import * as shared from '../../shared/index.js';
export const nameMap = new Map();
export const sharedMap = new Map();
Object.keys(shared).forEach( key => {
const value = shared[ key ]; // eslint-disable-line import/namespace
if ( typeof value === 'function' ) {
nameMap.set( value.name, key );
}
sharedMap.set( key, value.toString() );
});

@ -0,0 +1,36 @@
const fs = require( 'fs' );
const path = require( 'path' );
const acorn = require( 'acorn' );
const declarations = {};
fs.readdirSync( __dirname ).forEach( file => {
if ( !/^[a-z]+\.js$/.test( file ) ) return;
const source = fs.readFileSync( path.join( __dirname, file ), 'utf-8' );
const ast = acorn.parse( source, {
ecmaVersion: 6,
sourceType: 'module'
});
ast.body.forEach( node => {
if ( node.type !== 'ExportNamedDeclaration' ) return;
const { declaration } = node;
if ( !declaration ) return;
const name = declaration.type === 'VariableDeclaration' ?
declaration.declarations[0].id.name :
declaration.id.name;
const value = declaration.type === 'VariableDeclaration' ?
declaration.declarations[0].init :
declaration;
const { start, end } = value;
declarations[ name ] = source.slice( start, end );
});
});
fs.writeFileSync( 'src/generators/dom/shared.js', `// this file is auto-generated, do not edit it
export default ${JSON.stringify( declarations, null, '\t' )};` );
Loading…
Cancel
Save