From 39dcfc62bd1faafe1b413e3e8ef9cdf63133307d Mon Sep 17 00:00:00 2001 From: Josh Thomas Date: Tue, 13 Feb 2018 16:12:00 -0600 Subject: [PATCH] fix(): add json resolution plugin for rollup to stencil. --- src/compiler/bundle/rollup-bundle.ts | 2 + src/compiler/bundle/rollup-plugins/json.ts | 195 ++++++++++++++++++ .../rollup-plugins/makeLegalIdentifier.ts | 17 ++ .../bundle/test/bundle-modules.spec.ts | 33 +++ 4 files changed, 247 insertions(+) create mode 100644 src/compiler/bundle/rollup-plugins/json.ts create mode 100644 src/compiler/bundle/rollup-plugins/makeLegalIdentifier.ts diff --git a/src/compiler/bundle/rollup-bundle.ts b/src/compiler/bundle/rollup-bundle.ts index ca1976b2157..87b36f55da5 100644 --- a/src/compiler/bundle/rollup-bundle.ts +++ b/src/compiler/bundle/rollup-bundle.ts @@ -8,6 +8,7 @@ import transpiledInMemoryPlugin from './rollup-plugins/transpiled-in-memory'; import bundleEntryFile from './rollup-plugins/bundle-entry-file'; import { InputOptions, OutputChunk, rollup } from 'rollup'; import nodeEnvVars from './rollup-plugins/node-env-vars'; +import bundleJson from './rollup-plugins/json'; export async function createBundle(config: Config, compilerCtx: CompilerCtx, buildCtx: BuildCtx, entryModules: EntryModule[]) { @@ -28,6 +29,7 @@ export async function createBundle(config: Config, compilerCtx: CompilerCtx, bui include: 'node_modules/**', sourceMap: false }), + bundleJson(config), globals(), builtins(), bundleEntryFile(config, entryModules), diff --git a/src/compiler/bundle/rollup-plugins/json.ts b/src/compiler/bundle/rollup-plugins/json.ts new file mode 100644 index 00000000000..7a2411c7852 --- /dev/null +++ b/src/compiler/bundle/rollup-plugins/json.ts @@ -0,0 +1,195 @@ +import makeLegalIdentifier from './makeLegalIdentifier'; +import { Config } from '../../../declarations'; + +export interface Options { + indent?: string; + preferConst?: boolean; +} +export interface ASTNode { + type: string; + sourceType?: string; + start: number | null; + end: number | null; + body?: any[]; + declaration?: any; +} + +export default function bundleJson(config: Config, options: Options = {}) { + const path = config.sys.path; + + return { + name: 'json', + + resolveId(importee: string, importer: string): any { + if (importer && importer.startsWith(config.collectionDir) && importee.endsWith('.json')) { + return path.resolve( + path.dirname(importer).replace(config.collectionDir, config.srcDir), + importee + ); + } + + return null; + }, + + transform(json: string, id: string) { + if (id.slice(-5) !== '.json') return null; + + const data = JSON.parse(json); + let code = ''; + + const ast: ASTNode = { + type: 'Program', + sourceType: 'module', + start: 0, + end: null, + body: [] + }; + + if (Object.prototype.toString.call(data) !== '[object Object]') { + code = `export default ${json};`; + + ast.body.push({ + type: 'ExportDefaultDeclaration', + start: 0, + end: code.length, + declaration: { + type: 'Literal', + start: 15, + end: code.length - 1, + value: null, + raw: 'null' + } + }); + } else { + const indent = 'indent' in options ? options.indent : '\t'; + + const validKeys: string[] = []; + const invalidKeys: string[] = []; + + Object.keys(data).forEach(key => { + if (key === makeLegalIdentifier(key)) { + validKeys.push(key); + } else { + invalidKeys.push(key); + } + }); + + let char = 0; + + validKeys.forEach(key => { + const declarationType = options.preferConst ? 'const' : 'var'; + const declaration = `export ${declarationType} ${key} = ${JSON.stringify(data[key])};`; + + const start = char; + const end = start + declaration.length; + + // generate fake AST node while we're here + ast.body.push({ + type: 'ExportNamedDeclaration', + start: char, + end: char + declaration.length, + declaration: { + type: 'VariableDeclaration', + start: start + 7, // 'export '.length + end, + declarations: [ + { + type: 'VariableDeclarator', + start: start + 7 + declarationType.length + 1, // `export ${declarationType} `.length + end: end - 1, + id: { + type: 'Identifier', + start: start + 7 + declarationType.length + 1, // `export ${declarationType} `.length + end: start + 7 + declarationType.length + 1 + key.length, // `export ${declarationType} ${key}`.length + name: key + }, + init: { + type: 'Literal', + start: start + + 7 + + declarationType.length + + 1 + + key.length + + 3, // `export ${declarationType} ${key} = `.length + end: end - 1, + value: null, + raw: 'null' + } + } + ], + kind: declarationType + }, + specifiers: [], + source: null + }); + + char = end + 1; + code += `${declaration}\n`; + }); + + const defaultExportNode: ASTNode = { + type: 'ExportDefaultDeclaration', + start: char, + end: null, + declaration: { + type: 'ObjectExpression', + start: char + 15, + end: null, + properties: [] + } + }; + + char += 17 + indent.length; // 'export default {\n\t'.length' + + const defaultExportRows = validKeys + .map(key => { + const row = `${key}: ${key}`; + + const start = char; + const end = start + row.length; + + defaultExportNode.declaration.properties.push({ + type: 'Property', + start, + end, + method: false, + shorthand: false, + computed: false, + key: { + type: 'Identifier', + start, + end: start + key.length, + name: key + }, + value: { + type: 'Identifier', + start: start + key.length + 2, + end, + name: key + }, + kind: 'init' + }); + + char += row.length + (2 + indent.length); // ',\n\t'.length + + return row; + }) + .concat( + invalidKeys.map(key => `"${key}": ${JSON.stringify(data[key])}`) + ); + + code += `export default {\n${indent}${defaultExportRows.join(`,\n${indent}`)}\n};`; + ast.body.push(defaultExportNode); + + const end = code.length; + + defaultExportNode.declaration.end = end - 1; + defaultExportNode.end = end; + } + + ast.end = code.length; + + return { ast, code, map: { mappings: '' } }; + } + }; +} diff --git a/src/compiler/bundle/rollup-plugins/makeLegalIdentifier.ts b/src/compiler/bundle/rollup-plugins/makeLegalIdentifier.ts new file mode 100644 index 00000000000..0c62ef6c2d6 --- /dev/null +++ b/src/compiler/bundle/rollup-plugins/makeLegalIdentifier.ts @@ -0,0 +1,17 @@ +const reservedWords = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public'.split( ' ' ); +const builtins = 'arguments Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl'.split( ' ' ); + +const blacklisted: { [ key: string]: boolean} = {}; +reservedWords.concat(builtins).forEach(word => blacklisted[word] = true ); + +export default function makeLegalIdentifier(str: string) { + str = str + .replace( /-(\w)/g, ( _, letter ) => letter.toUpperCase() ) + .replace( /[^$_a-zA-Z0-9]/g, '_' ); + + if ( /\d/.test( str[0] ) || blacklisted[str] ) { + str = `_${str}`; + } + + return str; +} diff --git a/src/compiler/bundle/test/bundle-modules.spec.ts b/src/compiler/bundle/test/bundle-modules.spec.ts index f0f55ed968e..643bf70af45 100644 --- a/src/compiler/bundle/test/bundle-modules.spec.ts +++ b/src/compiler/bundle/test/bundle-modules.spec.ts @@ -75,6 +75,39 @@ describe('bundle-module', () => { ); }); + it('should include json files', async () => { + c.config.bundles = [ + { components: ['cmp-a'] }, + { components: ['cmp-b'] } + ]; + await c.fs.writeFiles({ + '/src/cmp-a.tsx': ` + import json from './package.json'; + console.log(json.thename); + @Component({ tag: 'cmp-a' }) export class CmpA {} + `, + '/src/cmp-b.tsx': ` + import json from './package.json'; + console.log(json.thename); + @Component({ tag: 'cmp-b' }) export class CmpB {} + `, + '/src/package.json': ` + { + "thename": "test" + } + ` + }); + await c.fs.commit(); + + const r = await c.build(); + expect(r.diagnostics).toEqual([]); + expectFilesWritten(r, + '/www/build/app/cmp-a.js', + '/www/build/app/cmp-b.js', + '/www/build/app/chunk1.js' + ); + }); + var c: TestingCompiler; beforeEach(async () => {