Skip to content

Commit e796fef

Browse files
committed
stringify helpers before bundling
1 parent dee8694 commit e796fef

File tree

4 files changed

+48
-24
lines changed

4 files changed

+48
-24
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
"precodecov": "npm run coverage",
1818
"lint": "eslint src test/*.js",
1919
"build": "npm run build:main && npm run build:shared && npm run build:ssr",
20-
"build:main": "rollup -c rollup/rollup.config.main.js",
20+
"build:main": "node shared/_build.js && rollup -c rollup/rollup.config.main.js",
2121
"build:shared": "rollup -c rollup/rollup.config.shared.js",
2222
"build:ssr": "rollup -c rollup/rollup.config.ssr.js",
23-
"dev": "rollup -c rollup/rollup.config.main.js -w",
23+
"dev": "node shared/_build.js && rollup -c rollup/rollup.config.main.js -w",
2424
"dev:shared": "rollup -c rollup/rollup.config.shared.js -w",
2525
"pretest": "npm run build",
2626
"prepublish": "npm run lint && npm run build"

src/generators/dom/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { walk } from 'estree-walker';
66
import deindent from '../../utils/deindent.js';
77
import CodeBuilder from '../../utils/CodeBuilder.js';
88
import visit from './visit.js';
9-
import { nameMap, sharedMap } from './sharedNames.js';
9+
import shared from './shared.js';
1010
import Generator from '../Generator.js';
1111
import preprocess from './preprocess.js';
1212

@@ -25,7 +25,7 @@ class DomGenerator extends Generator {
2525
}
2626

2727
helper ( name ) {
28-
if ( this.options.dev && sharedMap.has( `${name}Dev` ) ) {
28+
if ( this.options.dev && `${name}Dev` in shared ) {
2929
name = `${name}Dev`;
3030
}
3131

@@ -275,20 +275,20 @@ export default function dom ( parsed, source, options ) {
275275
);
276276
} else {
277277
generator.uses.forEach( key => {
278-
const str = sharedMap.get( key );
278+
const str = shared[ key ];
279279
const code = new MagicString( str );
280-
const fn = parseExpressionAt( str, 0 );
280+
const expression = parseExpressionAt( str, 0 );
281281

282-
let scope = annotateWithScopes( fn );
282+
let scope = annotateWithScopes( expression );
283283

284-
walk( fn, {
284+
walk( expression, {
285285
enter ( node, parent ) {
286286
if ( node._scope ) scope = node._scope;
287287

288288
if ( node.type === 'Identifier' && isReference( node, parent ) && !scope.has( node.name ) ) {
289-
if ( nameMap.has( node.name ) ) {
289+
if ( node.name in shared ) {
290290
// this helper function depends on another one
291-
const dependency = nameMap.get( node.name );
291+
const dependency = node.name;
292292
generator.uses.add( dependency );
293293

294294
const alias = generator.alias( dependency );
@@ -309,8 +309,8 @@ export default function dom ( parsed, source, options ) {
309309
`var ${generator.alias( 'transitionManager' )} = window.${global} || ( window.${global} = ${code});`
310310
);
311311
} else {
312-
const alias = generator.alias( fn.id.name );
313-
if ( alias !== fn.id.name ) code.overwrite( fn.id.start, fn.id.end, alias );
312+
const alias = generator.alias( expression.id.name );
313+
if ( alias !== expression.id.name ) code.overwrite( expression.id.start, expression.id.end, alias );
314314

315315
builders.main.addBlock( code.toString() );
316316
}

src/generators/dom/sharedNames.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/shared/_build.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const fs = require( 'fs' );
2+
const path = require( 'path' );
3+
const acorn = require( 'acorn' );
4+
5+
const declarations = {};
6+
7+
fs.readdirSync( __dirname ).forEach( file => {
8+
if ( !/^[a-z]+\.js$/.test( file ) ) return;
9+
10+
const source = fs.readFileSync( path.join( __dirname, file ), 'utf-8' );
11+
const ast = acorn.parse( source, {
12+
ecmaVersion: 6,
13+
sourceType: 'module'
14+
});
15+
16+
ast.body.forEach( node => {
17+
if ( node.type !== 'ExportNamedDeclaration' ) return;
18+
19+
const { declaration } = node;
20+
if ( !declaration ) return;
21+
22+
const name = declaration.type === 'VariableDeclaration' ?
23+
declaration.declarations[0].id.name :
24+
declaration.id.name;
25+
26+
const value = declaration.type === 'VariableDeclaration' ?
27+
declaration.declarations[0].init :
28+
declaration;
29+
30+
const { start, end } = value;
31+
declarations[ name ] = source.slice( start, end );
32+
});
33+
});
34+
35+
fs.writeFileSync( 'src/generators/dom/shared.js', `// this file is auto-generated, do not edit it
36+
export default ${JSON.stringify( declarations, null, '\t' )};` );

0 commit comments

Comments
 (0)