-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dea8230
commit 409fdec
Showing
10 changed files
with
170 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./dist/plugin'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { ReplaceSource } from 'webpack-sources'; | ||
import * as path from 'path'; | ||
import * as webpack from 'webpack'; | ||
import { createHash } from 'crypto'; | ||
import { store } from './store'; | ||
|
||
class LoaderPlugin { | ||
public static pluginName = 'component-controls-loader-plugin'; | ||
private readonly compilationHash: string; | ||
|
||
constructor() { | ||
const hash = createHash('md5') | ||
.update(new Date().getTime().toString()) | ||
.digest('hex'); | ||
this.compilationHash = `__${hash.substr(0, 6)}__`; | ||
} | ||
|
||
apply(compiler: webpack.Compiler) { | ||
this.replaceRuntimeModule(compiler); | ||
compiler.hooks.compilation.tap(LoaderPlugin.pluginName, compilation => { | ||
compilation.hooks.optimizeChunkAssets.tap( | ||
LoaderPlugin.pluginName, | ||
chunks => { | ||
chunks.forEach(chunk => { | ||
chunk.files | ||
.filter(fileName => fileName.endsWith('.js')) | ||
.forEach(file => { | ||
this.replaceSource(compilation, file); | ||
}); | ||
}); | ||
}, | ||
); | ||
}); | ||
} | ||
|
||
private replaceRuntimeModule(compiler: webpack.Compiler) { | ||
const nmrp = new webpack.NormalModuleReplacementPlugin( | ||
/story-store-data\.js$/, | ||
(resource: any) => { | ||
if (resource.resource) { | ||
resource.loaders.push({ | ||
loader: path.join(__dirname, 'runtimeLoader.js'), | ||
options: JSON.stringify({ compilationHash: this.compilationHash }), | ||
}); | ||
} | ||
}, | ||
); | ||
nmrp.apply(compiler); | ||
} | ||
|
||
private replaceSource( | ||
compilation: webpack.compilation.Compilation, | ||
file: string, | ||
) { | ||
const placeholder = `__COMPILATION_HASH__${this.compilationHash}`; | ||
const source = compilation.assets[file]; | ||
const placeholderPos = source.source().indexOf(placeholder); | ||
if (placeholderPos > -1) { | ||
const newContent = JSON.stringify(store); | ||
const newSource = new ReplaceSource(source, file); | ||
newSource.replace( | ||
placeholderPos, | ||
placeholderPos + placeholder.length - 1, | ||
newContent, | ||
); | ||
compilation.assets[file] = newSource; | ||
} | ||
} | ||
} | ||
|
||
module.exports = LoaderPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
export interface StoryPath { | ||
absPath: string; | ||
relPath: string; | ||
} | ||
|
||
export const replaceSource = (stories: StoryPath[], hashKey: string) => { | ||
const imports = ` | ||
const imports = {}; | ||
${stories | ||
.map(story => `imports['${story.absPath}'] = require(${story.relPath});`) | ||
.join('\n')} | ||
`; | ||
const storeConst = `const store = ${hashKey};\n`; | ||
let loadStories = ` | ||
for (let i = 0; i < store.stores.length; i+= 1) { | ||
const s = store.stores[i]; | ||
const kinds = Object.keys(s.kinds); | ||
for (let j=0; j < kinds.length; j += 1) { | ||
const kind = s.kinds[kinds[j]]; | ||
if (imports.hasOwnProperty(kind.fileName)) { | ||
const exports = imports[kind.fileName]; | ||
try { | ||
Object.keys(exports).forEach(key => { | ||
const exported = exports[key]; | ||
if (key === 'default') { | ||
const { storySource, ...rest } = exported; | ||
Object.assign(kind, rest); | ||
} else { | ||
const story = s.stories[key]; | ||
if (story) { | ||
story.renderFn = exported; | ||
if (exported.story) { | ||
Object.assign(story, exported.story); | ||
} | ||
} | ||
} | ||
}); | ||
} catch (e) { | ||
console.error(\`unable to load module \${kind.moduleId}\`, e); | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
const exports = `module.exports = store;\n`; | ||
const hmr = ` | ||
if (module.hot) { | ||
${stories | ||
.map( | ||
story => ` | ||
module.hot.accept(${story.relPath}, function() { | ||
console.log('HMR',${story.relPath}); | ||
}) | ||
`, | ||
) | ||
.join('\n')} | ||
} | ||
`; | ||
const newContent = ` | ||
${imports} | ||
${storeConst} | ||
${loadStories} | ||
${hmr} | ||
${exports} | ||
`; | ||
return newContent; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,23 @@ | ||
import loaderUtils from 'loader-utils'; | ||
import { StoriesStore } from '@component-controls/specification'; | ||
import { store } from './store'; | ||
import { getConfiguration, extractStories } from '@component-controls/config'; | ||
import { stringifyRequest } from 'loader-utils'; | ||
import { replaceSource, StoryPath } from './replaceSource'; | ||
|
||
module.exports = function() { | ||
const storyFiles = store.stores.reduce( | ||
(acc: string[], s: Pick<StoriesStore, 'stories' | 'kinds'>) => { | ||
return [ | ||
...acc, | ||
...Object.keys(s.kinds).map(key => | ||
//@ts-ignore | ||
loaderUtils.stringifyRequest(this, s.kinds[key].fileName), | ||
), | ||
]; | ||
}, | ||
[], | ||
module.exports = function(content: string) { | ||
//@ts-ignore | ||
const params = JSON.parse(this.query.slice(1)); | ||
//@ts-ignore | ||
const config = getConfiguration(this.rootContext); | ||
const stories: StoryPath[] = (config ? extractStories(config) || [] : []).map( | ||
fileName => ({ | ||
absPath: fileName, | ||
//@ts-ignore | ||
relPath: stringifyRequest(this, fileName), | ||
}), | ||
); | ||
|
||
const imports = ` | ||
const imports = {}; | ||
${storyFiles | ||
.map((fileName, fileIdx) => `imports['i_${fileIdx}'] = require(${fileName});`) | ||
.join('\n')} | ||
`; | ||
const storeConst = `const store = ${JSON.stringify(store)};\n`; | ||
let loadStories = ` | ||
let storyIdx = 0; | ||
for (let i = 0; i < store.stores.length; i+= 1) { | ||
const s = store.stores[i]; | ||
const kinds = Object.keys(s.kinds); | ||
for (let j=0; j < kinds.length; j += 1) { | ||
const exports = imports[\`i_\${storyIdx}\`]; | ||
storyIdx += 1; | ||
const kind = s.kinds[kinds[j]]; | ||
try { | ||
Object.keys(exports).forEach(key => { | ||
const exported = exports[key]; | ||
if (key === 'default') { | ||
const { storySource, ...rest } = exported; | ||
Object.assign(kind, rest); | ||
} else { | ||
const story = s.stories[key]; | ||
if (story) { | ||
story.renderFn = exported; | ||
if (exported.story) { | ||
Object.assign(story, exported.story); | ||
} | ||
} | ||
} | ||
}); | ||
} catch (e) { | ||
console.error(\`unable to load module \${kind.moduleId}\`, e); | ||
} | ||
} | ||
} | ||
`; | ||
const exports = `module.exports = store;\n`; | ||
const hmr = ` | ||
if (module.hot) { | ||
module.hot.accept([${storyFiles.join(', ')}], function() { | ||
console.log('Accepting the updated modules'); | ||
}) | ||
} | ||
`; | ||
const newContent = ` | ||
${imports} | ||
${storeConst} | ||
${loadStories} | ||
${hmr} | ||
${exports} | ||
`; | ||
|
||
return newContent; | ||
content = replaceSource( | ||
stories, | ||
`__COMPILATION_HASH__${params.compilationHash}`, | ||
); | ||
return content; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters